package linked_list;

public class 训练计划3 {
    //给定一个头节点为 head 的单链表用于记录一系列核心肌群训练编号，请将该系列训练编号 倒序 记录于链表并返回。
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    //双指针
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            ListNode pre = null;
            ListNode cur = head;
            while (cur!=null){
                ListNode tmp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = tmp;
            }
            return pre;
        }
    }
    //递归
    class Solution1 {
        public ListNode trainningPlan(ListNode head) {
            return recur(head,null);
        }
        private ListNode recur(ListNode cur,ListNode pre){
            if(cur ==null)
                return pre;
            ListNode recur = recur(cur.next,cur);
            cur.next = pre;
            return recur;
        }
    }
}
